<template>
  <a-spin :spinning="spinning">
    <a-table :columns="columns" :data-source="datas" :loading="spinning">
      <a slot="action" slot-scope="item" @click="deleteItem(item)">删除</a>
      <p slot="expandedRowRender" slot-scope="record" style="margin: 0">
        <!-- <json-viewer :value="record"></json-viewer> -->
        <pre>{{ record }}</pre>
      </p>
    </a-table>
  </a-spin>
</template>

<script>
export default {
  data() {
    return {
      spinning: false,
      columns: [
        {
          title: "开始时间",
          dataIndex: "StartTime"
        },
        {
          title: "任务名称",
          dataIndex: "ReminderName"
        },
        {
          title: "函数名称",
          dataIndex: "FunctionModuleName"
        },
        {
          title: "任务周期",
          dataIndex: "PeriodOfMinutes"
        },
        {
          title: "最后运行时间",
          dataIndex: "LastRunTime"
        },
        {
          title: "是否运行成功",
          dataIndex: "IsRunSuccess"
        },
        {
          title: "错误信息",
          dataIndex: "ErrorInfo",
          ellipsis: true
        },
        {
          title: "动作",
          key: "action",
          scopedSlots: { customRender: "action" }
        }
      ],
      datas: []
    };
  },
  created() {
    var that = this;
    this.spinning = true;
    this.$cloud
      .command("iTool Reminder Get", {
          rows: 20,
          page: 1
        })
      .then(res => {
        this.spinning = false;

        console.log("Reminders:", res);

        that.datas = res.map(item => {
          item.key = item.ReminderName;
          item.IsRunSuccess = item.IsRunSuccess ? "成功" : "失败";
          return item;
        });
      })
      .catch(error => {
        this.spinning = false;
        console.error(error);
      });
  },
  methods: {
    deleteItem(item) {
      console.log(item);

      var that = this;

      this.$confirm({
        title: `Are you sure delete this ${item.ReminderName}?`,
        content: "删除后将从任务列表中移除",
        okText: "Yes",
        okType: "danger",
        cancelText: "No",
        onOk() {
          that.spinning = true;
          that.$cloud
              .command("iTool Reminder Un", item.ReminderName)
              .then(res => {
                console.log(res);
                that.spinning = false;
                for (var i = 0; i < that.datas.length; i++) {
                  if (that.datas[i].ReminderName == item.ReminderName) {
                    that.datas.splice(i, 1);
                  }
                }
              })
              .catch(error => {
                that.spinning = false;
                console.log(error);
              });
        },
        onCancel() {}
      });

      
    }
  }
};
</script>
